package cn.com.miaoto.business.controller;

import cn.com.miaoto.base.common.BaseController;
import cn.com.miaoto.base.common.PageResult;
import cn.com.miaoto.base.common.PageSearch;
import cn.com.miaoto.base.common.ReturnMsg;
import cn.com.miaoto.business.model.Custom;
import cn.com.miaoto.business.service.CustomService;
import cn.com.miaoto.business.vo.CustomInfo;
import cn.com.miaoto.model.LoginUser;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 客户信息
 * Created by guo.chen on 2018/9/17.
 */
@RestController
@RequestMapping("custom")
public class  CustomController extends BaseController {

    @Resource
    private CustomService customService;

    @GetMapping("selectPageList")
    public PageResult selectPageList(HttpServletRequest request, PageSearch pageSearch){
        Map params = getParameterMap(request);
        try{
            PageHelper.startPage(pageSearch.getPage(),pageSearch.getLimit());
            List<CustomInfo> customList = this.customService.selectCustomList(params);
            return new PageResult(customList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询客户列表失败");
            return new PageResult();
        }
    }

    @GetMapping("selectAll")
    public List<Custom> selectAll(){
        try{
            return this.customService.selectAll();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询客户列表失败");
            return null;
        }
    }

    /**
     * 按月份统计新增客户数量
     * @return
     */
    @GetMapping("countByMonth")
    public List<Map> countByMonth(HttpServletRequest request){
        Map params = getParameterMap(request);
        try{
            return this.customService.countByMonth(params);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("按月份统计新增客户数量失败");
            return null;
        }
    }

    /**
     * 按年份统计新增客户数量
     * @return
     */
    @GetMapping("countByYear")
    public List<Map> countByYear(HttpServletRequest request){
        Map params = getParameterMap(request);
        try{
            return this.customService.countByYear(params);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("按年份统计新增客户数量失败");
            return null;
        }
    }

    @GetMapping("selectByPrimaryKey")
    public ReturnMsg selectByPrimaryKey(Integer id){
        try{
            Custom custom = this.customService.selectByKey(id);
            return new ReturnMsg(true,custom);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据主键查询客户信息失败");
            return new ReturnMsg(false);
        }
    }

    @PostMapping("edit")
    public ReturnMsg insertOrUpdate(HttpSession session, Custom custom){
        LoginUser loginUser =(LoginUser) session.getAttribute("currentUser");
        try {
            if(StringUtils.isEmpty(custom.getId())){
                custom.setCreator(loginUser.getUserId());
                custom.setCreateTime(LocalDateTime.now());
                return new ReturnMsg(this.customService.saveNotNull(custom)>0,"新增客户信息成功");
            } else{
                custom.setCreator(loginUser.getUserId());
                return new ReturnMsg(this.customService.updateNotNull(custom)>0,"修改客户信息成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("编辑客户信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("delete")
    public ReturnMsg delete(Custom custom){
        try {
            return new ReturnMsg(this.customService.updateNotNull(custom)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除客户信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("deleteByIds")
    public ReturnMsg deleteByIds(String ids){
        try {
            return new ReturnMsg(this.customService.deleteByIds(ids)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除客户信息失败");
            return new ReturnMsg(false);
        }
    }
}
